package cn.itcast.service.impl;

import cn.itcast.dao.CourierDao;
import cn.itcast.domain.Courier;
import cn.itcast.domain.Standard;
import cn.itcast.service.CourierService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

@Service
public class CourierServiceImpl implements CourierService {
    @Autowired
    private CourierDao courierDao;

    @Override
    public void save(Courier courier) {
        courierDao.save(courier);
    }


    @Override
    @Transactional
    public void delBatch(List<String> ids) {
        for (String id : ids) {
            courierDao.delBatch(Integer.parseInt(id));
        }

    }

    @Override
    @Transactional
    public void restore(List<String> ids) {
        for (String id : ids) {
            courierDao.restore(Integer.parseInt(id));
        }
    }
    @Override
    public Page<Courier> pageQuery(Courier courier,int page,int rows) {
        Specification<Courier> specification=new Specification<Courier>() {
            @Override
            public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();

                // 简单单表查询
                if (StringUtils.isNotBlank(courier.getCourierNum())) {
                    Predicate p1 = cb.equal(
                            root.get("courierNum").as(String.class),
                            courier.getCourierNum());
                    list.add(p1);
                }
                if (StringUtils.isNotBlank(courier.getCompany())) {
                    Predicate p2 = cb.like(
                            root.get("company").as(String.class),
                            "%" + courier.getCompany() + "%");
                    list.add(p2);
                }
                if (StringUtils.isNotBlank(courier.getType())) {
                    Predicate p3 = cb.equal(root.get("type").as(String.class),
                            courier.getType());
                    list.add(p3);
                }
                // 多表查询
                Join<Courier, Standard> standardJoin = root.join("standard",
                        JoinType.INNER);
                if (courier.getStandard() != null
                        && StringUtils.isNotBlank(courier.getStandard()
                        .getName())) {
                    Predicate p4 = cb.like(
                            standardJoin.get("name").as(String.class), "%"
                                    + courier.getStandard().getName() + "%");
                    list.add(p4);
                }
                return cb.and(list.toArray(new Predicate[0]));

            }
        };
        PageRequest pageRequest = PageRequest.of(page-1, rows);
        Page<Courier> page1 = courierDao.findAll(specification,pageRequest);
        return page1;
    }

}
